﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

using Selenium;
using NUnit.Framework;

namespace SeleniumTest
{   
    [SetUpFixture]
    public class WebTestBase
    {
        protected string m_browserType;
        protected string m_basePath;
        protected string m_seleniumAddress;
        protected int m_seleniumPort;
        protected string m_seleniumSpeed;
        protected ISelenium m_selenium;

        private string GetSetting(string setting)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"selenium.config.xml");

            XmlNodeList nodes = doc.DocumentElement.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                if (node.Name == "setting")
                {
                    XmlAttributeCollection attributes = node.Attributes;
                    if (attributes.Item(0).Value == setting) return attributes.Item(1).Value;
                }
            }
            return "NULL";
        }

        [SetUp]
        public virtual void TestFixtureSetup()
        {

            // Read from selenium.config
            m_browserType = GetSetting("BrowserType");
            m_basePath = GetSetting("BaseUrlPath");
            //
            m_seleniumAddress = GetSetting("SeleniumAddress");
            m_seleniumPort = int.Parse(GetSetting("SeleniumPort"));
            m_seleniumSpeed = GetSetting("SeleniumSpeed");


            // Start up the selenium session, using config values
            m_selenium = new DefaultSelenium(m_seleniumAddress, m_seleniumPort, m_browserType, m_basePath);
            m_selenium.Start();
            // sets the speed of execution of GUI commands
            //m_selenium.SetSpeed(m_seleniumSpeed);
        }

        [TearDown]
        public void TeardownTest()
        {
            try
            {
                m_selenium.Stop();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
        }
    }
}
